package com.cuitrip.business.comment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.cuitrip.apiservice.ServiceBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.business.comment.model.Comment;
import com.cuitrip.business.home.IndexActivity;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.order.model.OrderItem;
import com.cuitrip.business.share.ShareChannelBuilder;
import com.cuitrip.business.share.model.ServiceShareObject;
import com.cuitrip.business.share.ui.ShareDialogFragment;
import com.cuitrip.business.tripservice.model.ServiceDetail;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.im.CtInfoNotificationMsgItemProvider;
import com.cuitrip.im.model.CTInformationNotificationMessage;
import com.cuitrip.im.proxy.CtRongInfoProxy;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.image.process.MakeImageUtil;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.MessageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created on 7/26.
 */
public class CommentSuccessActivity extends CustomUiFragmentActivity implements IProxyCallback {
    public static final String ORDER_INFO = "CommentSuccessActivity.ORDER_INFO";
    public static final String ENABLE_SHARE = "enable_share";
    public static final String RATTING_VALUE_KEY = "ratting_value";
    public static final long INTERVAL = 2000L; //防止连续点击的时间间隔
    private static long lastClickTime = 0L; //上一次点击的时间

    private Comment mComment;
    private boolean enableShare;
    private float ratingValue;

    @Bind(R.id.go_share)
    TextView shareView;

    @Bind(R.id.go_next_trip)
    TextView nextTrip;

    @Bind(R.id.tv_desc)
    TextView tvDesc;

    private ApiProxy apiProxy = new ApiProxy(this);
    private int score;

    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }
        mComment = (Comment) intent.getSerializableExtra(NPSRateActivity.COMMENT_KEY);
        if (mComment == null) {
            finish();
            return;
        }
        score = getIntent().getIntExtra(NPSRateActivity.NPS_SCORE, -1);
        enableShare = getIntent().getBooleanExtra(ENABLE_SHARE, false);
        ratingValue = getIntent().getFloatExtra(CommentActivity.RATING_VALUE, -1);
        super.onCreate(savedInstanceState, R.layout.ct_order_comment_suc);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        if ((ratingValue > 0 && ratingValue <= 3) || (score >= 0 && score <= 6)) {
            tvDesc.setText(R.string.review_operation_review_feedback_bad);
            shareView.setVisibility(View.GONE);
        } else {
            shareView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initData() {
        super.initData();
        String nickName = LoginInstance.getInstance().getUserInfo().getNick();
        CTInformationNotificationMessage message =
                CtRongInfoProxy.getInstance().getCommonOrderChangeMessage(CtInfoNotificationMsgItemProvider.CtParamKey.BOOK_MSG_REVIEWED, nickName, -1L, null);
        CtRongInfoProxy.getInstance().sendSystemMessage(mComment.getTargetId(), message, null);
    }

    @OnClick(R.id.go_next_trip)
    public void goNextTrip() {
        IndexActivity.gotoTab(this, IndexActivity.RECOMMEND_TAB_INDEX);
    }

//    private ShareUtil mShareUtil;

    @OnClick(R.id.go_share)
    public void goShare() {
        if (clickFilter()) {
            showLoading();
            ServiceBusiness.getServiceDetail(apiProxy, mComment.getSid(), 0);
        }
    }

    @Override
    public boolean initCloseTopBar() {
        return true;
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig customUiConfig = new CustomUiConfig();
        customUiConfig.mTopbarLeftAreaName = getString(R.string.back_icon);
        customUiConfig.mTopbarMiddleAreaName = getString(R.string.review_operation_review_feedback);
        return customUiConfig;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        /*UMSsoHandler ssoHandler = mShareUtil.mController.getConfig().getSsoHandler(requestCode) ;
        if(ssoHandler != null){
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }*/
    }

    private boolean clickFilter() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < INTERVAL) {
            return false;
        }
        lastClickTime = time;
        return true;
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response = (CtApiResponse) result.getData();
        if (ApiManager.GET_SERVICE_DETAIL.equals(response.getApiName())) {
            hideLoading();
            if (response != null && response.code == CtResponseCode.RESULT_OK) {
                if (response.result instanceof ServiceDetail) {
                    ServiceDetail serviceDetail = (ServiceDetail) response.result;
                    ServiceInfo mService = serviceDetail.getServiceInfo();
                    File file = ImageLoader.getInstance().getDiskCache().get(serviceDetail.getServiceInfo().getBackPic());
                    String imgPath = null;
                    if (file != null) {
                        imgPath = file.getPath();
                    }
                    if (!TextUtils.isEmpty(imgPath)) {
                        ShareDialogFragment shareDialogFragment = ShareDialogFragment.newInstance(ServiceShareObject.getInstance(mService.getSid(), mService.getName(), mService.getBackPic(), mService.getDescptWithnoPic(), imgPath), ShareChannelBuilder.TripShareChannel.TRIP_TYPE, MakeImageUtil.takeScreenShot(this));


                        showDialogFragment(shareDialogFragment);

                    }
                }
            } else {
                if (response != null && !TextUtils.isEmpty(response.msg)) {
                    MessageUtils.showToast(response.msg);
                }
            }
        }

        return false;
    }


}
